package com.itheima.controller;

import com.itheima.pojo.ResultInfo;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.HeiMaUtils;
import freemarker.template.utility.HtmlEscape;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.tags.HtmlEscapeTag;
import org.springframework.web.servlet.tags.HtmlEscapingAwareTag;
import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.JavaScriptUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Controller
@RequestMapping("/other")
@Slf4j
public class OtherController {
    @Autowired
    private UserService userService;

    @RequestMapping("/zl")
    public ModelAndView test01(String name, String avatar, String data, HttpServletResponse response) {
        log.debug("name1 = " + name);
        log.debug("avatar1 = " + avatar);
        log.debug("data1 = " + data);
        name = Optional.ofNullable(name).orElse("");
        avatar = Optional.ofNullable(avatar).orElse("logo2.png");
        data = Optional.ofNullable(data).orElse("abc");

        // 解决XSS
        name = HeiMaUtils.htmlEscape(name);
        avatar = HeiMaUtils.htmlEscape(avatar);
        data = HeiMaUtils.htmlJS(data);

        log.debug("name2 = " + name);
        log.debug("avatar2 = " + avatar);
        log.debug("data2 = " + data);
        return new ModelAndView("zhuli")
                .addObject("name", name)
                .addObject("avatar", avatar)
                .addObject("data", data);
    }

    @RequestMapping("/cookie")
    @ResponseBody
    public String test02(HttpServletResponse response) {
        response.addCookie(new Cookie("user", "abc"));
        response.addCookie(new Cookie("local", "广州"));
        return "写入Cookie成功"; // 模板名字
    }
}
